<template>
  <div>
    <el-table
      :data="dataList"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="ID"
      />
      <el-table-column
        v-if="getTabPosition"
        prop="username"
        label="师傅信息"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.username }}</div>
          <div>电话：{{ scope.row.mobile }}</div>
        </template>
      </el-table-column>
      <el-table-column
        v-if="!getTabPosition"
        prop="username"
        label="名字"
        :key="Math.random()"
      />
      <el-table-column
        prop="unionid"
        label="城市"
      />
      <el-table-column
        v-if="!getTabPosition"
        prop="isrealName"
        label="实名状态"
      />
      <el-table-column
        prop="createTime"
        label="入驻时间"
        width="180"
      />
      <el-table-column
        v-if="getTabPosition"
        label="个人信息"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="onOpenDetailInfo(scope.row)">查看个人信息</el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="getTabPosition"
        label="师傅评价"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="onOpenMasterComment(scope.row)">师傅评价</el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="!getTabPosition"
        prop="date"
        label="个人评价"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="onOpenUserComment(scope.row)">个人评价</el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="getTabPosition"
        prop="order_take_count"
        label="接单次数"
        :key="Math.random()"
      />
    
      <el-table-column
        v-if="getTabPosition"
        prop="earn_count"
        label="接单金额"
        :key="Math.random()"
      />
      <el-table-column
        v-if="!getTabPosition"
        prop="order_count"
        label="发单次数"
        :key="Math.random()"
      />
      <el-table-column
        v-if="!getTabPosition"
        prop="consume_count"
        label="发单金额"
        :key="Math.random()"
      />
      <el-table-column
        v-if="!getTabPosition"
        prop="contributeCount"
        label="累计贡献"
        :key="Math.random()"
      >
        <template slot-scope="scope">
          <el-link type="primary" size="small">{{ scope.row.contributeCount || '--' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        v-if="!getTabPosition"
        prop="level"
        label="会员等级"
        :key="Math.random()"
      />
      <el-table-column
        v-if="!getTabPosition"
        prop="level"
        label="操作"
        fixed="right"
        width="100"
        :key="Math.random()"
      >
        <template slot-scope="scope" v-if="scope.row.isrealName !== '已实名'">
          <el-button type="primary" size="small" @click="onApproval(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="个人信息管理"
      :visible.sync="dialogVisible"
      width="600px"
      :before-close="handleClose">
      <userDetailInfo :userInfo="userInfo"/>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="师傅评价"
      width="60%"
      :visible.sync="masterCommentVisible"
      :before-close="onCloseMasterComment">
      <userCommentDetail :userInfo="userInfo" :userCommentInfo="userCommentInfo" title="师傅"/>
    </el-dialog>
    <el-dialog
      title="个人评价"
      width="60%"
      :visible.sync="userCommentVisible"
      :before-close="onCloseUserComment">
      <userCommentDetail :userInfo="userInfo" :userCommentInfo="userCommentInfo" title="用户"/>
    </el-dialog>
  </div>
</template>

<script>
import { queryUserMasterInfo, auditUserMasterInfo } from '@/api/yzUser';
import userDetailInfo from './userInfo';
import userCommentDetail from './userComment';
import { getPageYzComment } from '@/api/yzComment';

export default ({
  name: 'MasterTable',
  components: {
    userDetailInfo,
    userCommentDetail
  },
  props: {
    tabPosition: String,
    dataList: Array
  },
  data() {
    return {
      dialogVisible: false,
      userInfo: {},
      masterCommentVisible: false,
      userCommentVisible: false,
      userCommentInfo: []
    }
  },
  computed: {
    getTabPosition() {
      return this.tabPosition === 'master'
    }
  },
  mounted() {

  },
  methods: {
    async onOpenDetailInfo(row) {
      const { data } = await queryUserMasterInfo(row.id);
      // console.log('onOpenDetailInfo', data.data)
      if (data.code === 200) {
        this.userInfo = data.data || {};
        this.dialogVisible = true;
      } else {
        this.$message({
          message: data.msg || '个人信息获取失败',
          type: 'error',
          showClose: true,
          duration: 3000,
        });
      }
    },
    handleClose() {
      this.dialogVisible = false;
    },
    async onOpenMasterComment(record) {
      // 获取评价
      const { data } = await getPageYzComment({ buserId: record.id, pageIndex: 1, pageSize: 500 });
      if (data.code === 200) {
        this.userCommentInfo = data.data.records;
        this.masterCommentVisible = true;
      } else {
        this.$message({
          message: data.msg || '评价信息获取失败',
          type: 'error',
          showClose: true,
          duration: 3000,
        });
      }
    },
    onCloseMasterComment() {
      this.masterCommentVisible = false; 
    },
    async onOpenUserComment(row) {
      const { data } = await getPageYzComment({ ouserId: row.id, pageIndex: 1, pageSize: 500 });
      if (data.code === 200) {
        this.$message({
          message: data.msg,
          type: 'success',
        });
        this.userCommentInfo = data.data.records || [];
        this.userCommentVisible = true;
      } else {
        this.$message({
          message: data.msg || '评价信息获取失败',
          type: 'error',
          showClose: true,
          duration: 3000,
        });
      }
    },
    onCloseUserComment() {
      this.userCommentVisible = false;
    },
    async onApproval(rowData) {
      this.$confirm('确认已实名认证吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success',
      }).then(action => {
        this.fetchApproval(rowData);
      })
    },
    async fetchApproval(rowData) {
      const { data } = await auditUserMasterInfo({ id: rowData.id, status: 1, isrealName: '已实名' })
      if (data.code === 200) {
        this.$message({
          message: data.msg || '审核成功',
          type: 'success',
        });
        this.$emit('refreshList')
      } else {
        this.$message({
          message: data.msg || '失败',
          type: 'error',
          showClose: true,
          duration: 3000,
        });
      }
    }
  }
})
</script>
